package com.avic.szzx.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.avic.szzx.controller.bean.NebulaConstant;
import com.avic.szzx.controller.bean.NebulaProperties;
import com.avic.szzx.exception.BusinessException;
import com.avic.szzx.exception.EmBusinessError;
import com.vesoft.nebula.client.graph.SessionPool;
import com.vesoft.nebula.client.graph.net.NebulaPool;
import com.vesoft.nebula.client.graph.net.Session;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
@Component
public class NebulaTemplate {
    @Resource
    NebulaPool nebulaPool;
    @Autowired
    private SessionPool sessionPool;
    @Resource
    NebulaProperties nebulaProperties;

    public JSONObject queryObject(String stmt) {
        JSONObject nebulaResult = executeObject(stmt);
        return nebulaResult;
    }

    public JSONObject executeObject(String stmt) {
        JSONObject jsonObject = executeJson(stmt);
        return jsonObject;
    }


    public JSONObject executeJson(String stmt) {
        JSONObject restJson = new JSONObject();
        JSONObject jsonObject = null;
        JSONObject results = null;
        if (!sessionPool.isActive()) {
            int idleSessionNums = sessionPool.getIdleSessionNums();
            log.error("session pool init failed,the idle session nums are：" + idleSessionNums);
            throw new BusinessException(EmBusinessError.UNKNOW_ERROR.getErrCode(), "session pool init failed,the idle session nums are：" + idleSessionNums);
        }
        try {
            jsonObject = JSON.parseObject(sessionPool.executeJson(stmt));
            log.info("JSONObject:" + jsonObject.toJSONString());
            JSONObject errors = jsonObject.getJSONArray(NebulaConstant.NebulaJson.ERRORS.getKey()).getJSONObject(0);
            restJson.put(NebulaConstant.NebulaJson.CODE.getKey(), errors.getInteger(NebulaConstant.NebulaJson.CODE.getKey()));
            if (errors.getInteger(NebulaConstant.NebulaJson.CODE.getKey()) != 0) {
                restJson.put(NebulaConstant.NebulaJson.MESSAGE.getKey(), errors.getString(NebulaConstant.NebulaJson.MESSAGE.getKey()));
                return restJson;
            }
            results = jsonObject.getJSONArray(NebulaConstant.NebulaJson.RESULTS.getKey()).getJSONObject(0);
        } catch (Exception e) {
            log.error("nebula execute err：", e);
        }
        return results;
    }

    public JSONObject executeNGQL(String stmt) {
        JSONObject restJson = new JSONObject();
        JSONObject results = null;
        Session session = null;
        try {
            JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(session).executeJson(stmt));
            log.info("JSONObject:" + jsonObject.toJSONString());
            JSONObject errors = jsonObject.getJSONArray(NebulaConstant.NebulaJson.ERRORS.getKey()).getJSONObject(0);
            restJson.put(NebulaConstant.NebulaJson.CODE.getKey(), errors.getInteger(NebulaConstant.NebulaJson.CODE.getKey()));
            if (errors.getInteger(NebulaConstant.NebulaJson.CODE.getKey()) != 0) {
                restJson.put(NebulaConstant.NebulaJson.MESSAGE.getKey(), errors.getString(NebulaConstant.NebulaJson.MESSAGE.getKey()));
                return restJson;
            }
            results = jsonObject.getJSONArray(NebulaConstant.NebulaJson.RESULTS.getKey()).getJSONObject(0);

        } catch (Exception e) {
            restJson.put(NebulaConstant.NebulaJson.CODE.getKey(), NebulaConstant.ERROR_CODE);
            restJson.put(NebulaConstant.NebulaJson.MESSAGE.getKey(), e.toString());
            log.error("nebula execute err：", e);
        }
        return results;
    }
}